#!/bin/bash
# Celery启动脚本（Linux/Mac）

echo "========================================"
echo "启动Celery Worker和Beat"
echo "========================================"
echo ""

# 检查Redis是否运行
echo "检查Redis连接..."
python3 -c "import redis; r = redis.Redis(host='127.0.0.1', port=6379, db=0); r.ping(); print('Redis连接正常')" 2>/dev/null
if [ $? -ne 0 ]; then
    echo "错误: Redis未运行或无法连接"
    echo "请先启动Redis服务: redis-server"
    exit 1
fi

# 获取脚本所在目录
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"

cd "$PROJECT_DIR"

echo ""
echo "启动Celery Worker..."
celery -A travel_plan worker --loglevel=info &
WORKER_PID=$!

sleep 3

echo ""
echo "启动Celery Beat..."
celery -A travel_plan beat --loglevel=info &
BEAT_PID=$!

echo ""
echo "========================================"
echo "Celery已启动"
echo "Worker PID: $WORKER_PID"
echo "Beat PID: $BEAT_PID"
echo ""
echo "停止服务: kill $WORKER_PID $BEAT_PID"
echo "========================================"

# 等待用户中断
trap "kill $WORKER_PID $BEAT_PID; exit" INT TERM
wait

